package com.lsd.game.lastrun.controller;


import java.util.HashMap;
import java.util.Map;

import com.lsd.game.lastrun.Constants;
import com.lsd.game.lastrun.model.Player;
import com.lsd.game.lastrun.model.World;

public class WorldController {
	
	enum Keys {
		UP, DOWN, SHIELD
	}
	
	private World world;
	private Player player;
	
	static Map<Keys, Boolean> keys = new HashMap<WorldController.Keys, Boolean>(); 
	static {
		keys.put(Keys.UP, false);
		keys.put(Keys.DOWN, false);
		keys.put(Keys.SHIELD, false);
	};
	
	public WorldController(World world) {
		this.world = world;
		this.player = (Player) world.getObjects().get(0); // First object SHOULD BE player!!
	}
	
	// Key presses and touches
	// Pressed methods
	public void upPressed() {
		keys.get(keys.put(Keys.UP, true));
	}
	
	public void downPressed() {
		keys.get(keys.put(Keys.DOWN, true));
	}
	
	public void shieldPressed() {
		keys.get(keys.put(Keys.SHIELD, true));
	}
	
	// Released methods
	public void upReleased() {
		keys.get(keys.put(Keys.UP, false));
	}
	
	public void downReleased() {
		keys.get(keys.put(Keys.DOWN, false));
	}
	
	public void shieldReleased() {
		keys.get(keys.put(Keys.SHIELD, false));
	}
	
	// Main update method
	public void update(float delta) {
		processInput();
		// player.update(delta);
	}
	
	// Change the player's state based on input
	private void processInput() {
		
		// Check if player is out of bounds
		if (player.getPosition().y > Constants.CAMERA_HEIGHT - player.getSize()) {
			upReleased();
		}
		if (player.getPosition().y < 0) {
			downReleased();
		}
		
		// Process key inputs
		if (keys.get(Keys.UP)) {
			player.setVelocity(player.getVelocity().x, player.getSpeed());
		}
		
		if (keys.get(Keys.DOWN)) {
			player.setVelocity(player.getVelocity().x, -player.getSpeed());
		}
		
		if ((keys.get(Keys.UP) && keys.get(Keys.DOWN)) || (!keys.get(Keys.UP) && !keys.get(Keys.DOWN))) {
			player.setVelocity(player.getVelocity().x, 0f);
		}
	}
}
